package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.Equipment;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/equipment")
@CrossOrigin
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;
    @GetMapping("/{lineId}")
    public Result findAllEquipmentByLineId(@PathVariable("lineId") int lineId) {
        List<Equipment> equipmentList=equipmentService.getAllEquipmentByLineId(lineId);
        if(equipmentList.isEmpty()){
            return Result.error("equipment not found");
        }else {
            return Result.success(equipmentList);
        }
    }
    @PostMapping("/addEquipment")
    public Result addEquipment(@RequestBody Equipment equipment) {
        try {
            equipmentService.addEquipment(equipment);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @PutMapping("updateEquipment")
    public Result updateEquipment(@RequestBody Equipment equipment) {
        try {
            equipmentService.updateEquipment(equipment);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @DeleteMapping("/{equipmentId}")
    public Result deleteEquipment(@PathVariable("equipmentId") Integer equipmentId) {
        try {
            equipmentService.deleteEquipmentByEquipmentId(equipmentId);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
